package com.qianbao.qianbaobusiness.ui.cashier.databill;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;


/**
 * Created by jiangyu on 15/9/30.
 */
public class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

    protected RecyclerView recyclerView;
    protected Context mContext;
    private RecyclerView.Adapter mAdapter;
    private OnRecyclerViewItemClickListener mItemClickListener;
    private OnRecyclerViewItemLongClickListener mItemLongClickListener;

    public BaseViewHolder(Context context, View rootView, BaseRecyclerViewAdapter mBaseRecyclerViewAdapter) {
        super(rootView);
        mContext = context;
        mAdapter = mBaseRecyclerViewAdapter;
        this.mItemClickListener = mBaseRecyclerViewAdapter.getClickListener();
        this.mItemLongClickListener = mBaseRecyclerViewAdapter.getLongClickListener();
        rootView.setOnClickListener(this);
        rootView.setOnLongClickListener(this);
    }

    public BaseViewHolder(Context context, View rootView, RecyclerView.Adapter adapter, OnRecyclerViewItemClickListener
            clickListener, OnRecyclerViewItemLongClickListener longClickListener) {
        super(rootView);
        mContext = context;
        mAdapter = adapter;
        this.mItemClickListener = clickListener;
        this.mItemLongClickListener = longClickListener;
        rootView.setOnClickListener(this);
        rootView.setOnLongClickListener(this);
    }

    public BaseViewHolder(RecyclerView recyclerView, View rootView, BaseRecyclerViewAdapter mBaseRecyclerViewAdapter) {
        super(rootView);
        mContext = recyclerView.getContext();
        mAdapter = mBaseRecyclerViewAdapter;
        this.mItemClickListener = mBaseRecyclerViewAdapter.getClickListener();
        this.mItemLongClickListener = mBaseRecyclerViewAdapter.getLongClickListener();
        rootView.setOnClickListener(this);
        rootView.setOnLongClickListener(this);
    }

    /**
     * 点击监听
     */
    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick(recyclerView, v, getAdapterPosition());
        }
    }

    /**
     * 长按监听
     */
    @Override
    public boolean onLongClick(View v) {
        if (mItemLongClickListener != null) {
            mItemLongClickListener.onItemLongClick(recyclerView, v, getAdapterPosition());
        }
        return true;
    }

    public void invalidate() {
        if (null != mAdapter && null != itemView) {
            mAdapter.notifyItemChanged(getAdapterPosition());
        }
    }
}